import * as common from "Commons/index";
/*
 * 
 * @param {订阅消息id} tmplIds 
 * @param {订阅消息类型} type 
 *      1 每日签到订阅
 * @param {是否加载loding} showLoading 
 */
async function subscribeMessage(tmplIds,type,userInfo,showLoading=false){
    if(showLoading){
        wx.showLoading({
            title: '加载中',
        })
    }
    return new Promise(async (resolve, reject)=>{
        requestSubscribeMessage(tmplIds,type,userInfo,showLoading,resolve, reject);
    })
}
// 设置每日签到消息提醒
function message(tmplIdsSuccess,type,userInfo,resolve, reject,showLoading){
    var pRseult = []
    for(let i=0;i<tmplIdsSuccess.length;i++){
        const all_integral = userInfo.integral||0;
        const continuous_day = userInfo.continuous_day || 0
        const integrals = common.integralIninData(continuous_day);
        const gender = userInfo.gender
        var data_info={
            value:[tmplIdsSuccess[i]],
            openId:userInfo.openId,
            add_integral:integrals[continuous_day%7].integral,
            nickName:userInfo.nickName,
            continuous_day: continuous_day,
            gender: gender,
            type:type.value,
            type_config:type.config,
            action: 'add_msg',
            userInfo:userInfo
        }
        pRseult.push(wx.cloud.callFunction({
            name: 'signin',
            data: data_info
        }))
    }
    Promise.all(pRseult).then(result=>{
        if(showLoading){
            wx.hideLoading();
        }
        var bol=true;
        console.log(result)
        result.forEach(obj=>{
            if(obj.errMsg != 'cloud.callFunction:ok'){
                bol = false
            }
        })
        if(bol){
            resolve(true)
        }else{
            reject(false)
        }
    })
}
/***
 * type.value 1签到任务
 */
function typeEvents(tmplIdsSuccess,type,userInfo,resolve, reject,showLoading){
    // 每日签到订阅
    if(type.value == 1){
        message(tmplIdsSuccess,type,userInfo,resolve, reject,showLoading)
        return
    }
    // 订单未支付提醒
    if(type.value == 2){
        message(tmplIdsSuccess,type,userInfo,resolve, reject,showLoading)
        return
    }
}
// 微信一次性订阅消息授权设置
async function requestSubscribeMessage(tmplIds,type,userInfo,showLoading=false,resolve, reject){
    // return new Promise(async (resolve, reject)=>{
       var res = await wx.requestSubscribeMessage({
            tmplIds: tmplIds
        })
        if(showLoading){
            wx.hideLoading();
        }
        if (res.errMsg == "requestSubscribeMessage:ok") {
            if (res[tmplIds[0]] == 'accept') {
                // 获取授权订阅的模板 id
                var tmplIdsSuccess = [];
                for (const key in res) {
                    if (res.hasOwnProperty(key)) {
                        const element = res[key];
                        if(element == "accept"){
                            tmplIdsSuccess.push(key)
                        }
                    }
                }
                // typeEvents(tmplIdsSuccess,type,userInfo,resolve, reject,showLoading)
                resolve(res)
            } else if (res[tmplIds[0]] == 'reject') {
                // 拒接订阅
                wx.showToast({
                    title: '设置订阅失败！',
                    icon: 'none',
                    duration: 2000
                })
                resolve(false)
            } else if (res[tmplIds[0]] == 'ban') {
                // '表示已被后台封禁
                wx.showModal({
                    content: "设置提醒异常，请联系管理员！",
                    showCancel: false
                })
                resolve(false)
            }
        }else{
            wx.showModal({
                content: res.errMsg,
                showCancel: false
            })
            resolve(false)
        }
    // })
}
module.exports={
    subscribeMessage:subscribeMessage,
    requestSubscribeMessage:requestSubscribeMessage
}
